package cn.edu.fzu.homemaking.remoting;

import cn.edu.fzu.homemaking.redis.Redis;
import cn.edu.fzu.homemaking.remoting.dto.IpAddressDto;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

/**
 * @author fangxin
 * @date 2022/11/23
 */
@Component
public class IpAddressApiImpl extends LocationApi<IpAddressDto> {

    private static final String IP  = "__IP__";

    private static final String URL = "http://ip.taobao.com/outGetIpInfo?ip=" + IP + "&accessKey=alibaba-inc";

    @Override
    public IpAddressDto transfer(JSONObject result) {
        JSONObject data = result.getJSONObject("data");
        return data.toJavaObject(IpAddressDto.class);
    }


    @Redis("ip:%s")
    public IpAddressDto get(String ip) {
        return super.get(URL.replace(IP, ip));
    }

}
